package com.ruoyi.plus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.plus.domain.SysArea;
import com.ruoyi.plus.mapper.SysAreaMapper;
import com.ruoyi.plus.service.ISysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 区域Service业务层处理
 *
 * @author 李坤
 * @date 2020-02-09
 */
@Service("sysAreaService")
public class SysAreaServiceImpl extends ServiceImpl<SysAreaMapper, SysArea> implements ISysAreaService {

    @Autowired
    private SysAreaMapper sysAreaMapper;

    /**
     * 查询区域列表
     *
     * @param sysArea 区域
     * @return 区域
     */
    @Override
    public List<SysArea> selectSysAreaList(SysArea sysArea) {
        return sysAreaMapper.selectSysAreaList(sysArea);
    }


    /**
     * 查询区域树列表
     *
     * @return 所有区域信息
     */
    @Override
    public List<Ztree> selectSysAreaTree() {
        List<SysArea> sysAreaList = sysAreaMapper.selectSysAreaList(new SysArea());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (SysArea sysArea : sysAreaList) {
            Ztree ztree = new Ztree();
            ztree.setId(sysArea.getId());
            ztree.setpId(sysArea.getParentId());
            ztree.setName(sysArea.getAreaName());
            ztree.setTitle(sysArea.getAreaName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public List<SysArea> selectAllProvince() {
        SysArea sysArea = new SysArea();
        sysArea.setParentId(0L);
        Map<String, Object> map = new HashMap(2);
        map.put("parent_id", 0);

        List<SysArea> areas = sysAreaMapper.selectByMap(map);
        return areas;
    }

    @Override
    public List<SysArea> selectByParentId(int parentId) {
        return sysAreaMapper.selectByParentId(parentId);
    }

    @Override
    public boolean save(SysArea sysArea) {
        if (sysArea.getParentId() == null || sysArea.getParentId().equals("")) {
            sysArea.setAncestors("0");
        } else {
            SysArea parentArea = this.getById(sysArea.getParentId());
            if (parentArea != null) {
                sysArea.setAncestors(parentArea.getAncestors() + "," + sysArea.getParentId());
            } else {
                sysArea.setAncestors("0");
            }
        }
        return super.save(sysArea);
    }

    @Override
    public boolean saveOrUpdate(SysArea sysArea) {
        if (sysArea.getParentId() == null || sysArea.getParentId().equals("")) {
            sysArea.setAncestors("0");
        } else {
            SysArea parentArea = this.getById(sysArea.getParentId());
            if (parentArea != null) {
                sysArea.setAncestors(parentArea.getAncestors() + "," + sysArea.getParentId());
            } else {
                sysArea.setAncestors("0");
            }
        }
        return super.saveOrUpdate(sysArea);
    }
}
